package jsu.xxy.coffee3.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.servlet.http.HttpServletResponse;
import jsu.xxy.coffee3.Utils.ExcelOutUtil;
import jsu.xxy.coffee3.Utils.ExcelUtils;
import jsu.xxy.coffee3.domain.Ids;
import jsu.xxy.coffee3.domain.MaterialNoId;
import jsu.xxy.coffee3.domain.SupplierAndMaterials;
import jsu.xxy.coffee3.mapper.SupplierMapper;
import jsu.xxy.coffee3.service.impl.SupplierServiceImpl;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    SupplierServiceImpl supplierService;


    @PostMapping("/addSupplierExcel")
    @Operation(summary = "@RequestParam传一个文件供应商 excel file")
    public boolean addSupplierExcel(@RequestParam MultipartFile file){
        List<SupplierAndMaterials> supplierAndMaterials = ExcelUtils.excelToSupplierAndMaterials(file);
        for (int i = 0 ;i < supplierAndMaterials.size() ; i ++){
//            System.out.println(supplierAndMaterials.get(i));
            supplierService.addInform(supplierAndMaterials.get(i));
        }
        return true;
    }


    @GetMapping("/getSupplierExcelMb")
    @Operation(summary = "导出excel模板，供应商 ,")
    public void getSupplierExcelMb(HttpServletResponse response){
        XSSFWorkbook wb = ExcelOutUtil.getDeviceDataByExcelSupplierMb();
        String fileName = "deviceData.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/getSupplierExcel")
    @Operation(summary = "导出excel，供应商全部信息 ,")
    public void getExcel(HttpServletResponse response){
        XSSFWorkbook wb = ExcelOutUtil.getDeviceDataByExcelSupplier(supplierService.getAllNoPage());

        String fileName = "deviceData.xlsx";
        OutputStream outputStream = null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //设置ContentType请求信息格式
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/getAll")
    @Operation(summary = "查所有供应商信息")
    public Page<SupplierAndMaterials> getAll(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        return supplierService.getAll(pageNo,pageSize);

    }

    @GetMapping("/getByName/{name}")
    @Operation(summary = "按名字查询（原材料）")
    public Page<SupplierAndMaterials> selectByName(@PathVariable String name,@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return supplierService.selectByName(name,pageNum,pageSize);

    }

    @GetMapping("/getByNameSupplier/{nameSupplier}")
    @Operation(summary = "按名字查询(供应商）")
    public List<SupplierAndMaterials> selectByNameSupplier(@PathVariable String nameSupplier,@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return supplierService.selectByNameSupplier(nameSupplier,pageNo,pageSize);

    }

    @DeleteMapping("/deleteById")
    @Operation(summary = "按id删除（商品mid，供应商supplierid）")
        public  boolean deleteById(@RequestParam Integer mid,
                                             @RequestParam Integer supplierid) {
        return supplierService.deleteById(mid,supplierid);

    }
//    @DeleteMapping("/deleteByIds")
//    @Operation(summary = "按id删除（商品mid，供应商supplierid）")
//    public  boolean deleteByIds(@RequestParam Integer mid,
//                               @RequestParam Integer supplierid) {
//        return supplierService.deleteById(mid,supplierid);
//
//    }
    @PostMapping("/addInform")
    @Operation(summary = "增加所有信息")
    public boolean addInform(@RequestBody SupplierAndMaterials supplierAndMaterials){

        return supplierService.addInform(supplierAndMaterials);
    }

}
